package com.feihong.yw189.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;

import com.feihong.yw189.R;
import com.feihong.yw189.adapter.holder.ExpandAbleListHolder;
import com.feihong.yw189.model.Spec.SpecInfoModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by PVer on 2018/1/17.
 */

public class ExpandAbleListAdapter extends BaseExpandableListAdapter implements ExpandableListAdapter {

    private List<SpecInfoModel.DataBean> mGoups;
    private LayoutInflater mInlater;
    private Context mContext;
    private static final int GROUP_ITEM_RESOURCE = R.layout.item_expandlist;
    private static final int CHILD_ITEM_RESOURCE = R.layout.item_expandchildlist;

    public ExpandAbleListAdapter(Context context) {

        mGoups = new ArrayList<>();
        this.mContext = context;
        this.mInlater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setData(List<SpecInfoModel.DataBean> groups){
        mGoups = groups;
        notifyDataSetChanged();
    }
    @Override
    public int getGroupCount() {
        return mGoups.size();
    }

    @Override
    public int getChildrenCount(int groupPostion) {
        List<SpecInfoModel.DataBean.SpecListBean> specListBeans = mGoups.get(groupPostion).getSpec_list();
        if(specListBeans != null){
            return mGoups.get(groupPostion).getSpec_list().size();
        }
        return 0;
    }

    @Override
    public SpecInfoModel.DataBean getGroup(int groupPosition) {
        return mGoups.get(groupPosition);
    }

    @Override
    public SpecInfoModel.DataBean.SpecListBean getChild(int groupPosition, int childPosition) {
        return mGoups.get(groupPosition).getSpec_list().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
        ExpandAbleListHolder holder;
        if (convertView == null) {
            convertView = mInlater.inflate(GROUP_ITEM_RESOURCE, viewGroup,false);
            holder = new ExpandAbleListHolder();
            holder.initParentView(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ExpandAbleListHolder) convertView.getTag();
        }
        holder.initParentData(mGoups.get(groupPosition));
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ExpandAbleListHolder holder;
        if(convertView == null){
            convertView = mInlater.inflate(CHILD_ITEM_RESOURCE, parent,false);
            holder = new ExpandAbleListHolder();
            holder.initChildView(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ExpandAbleListHolder) convertView.getTag();
        }
        holder.initChildData(mGoups.get(groupPosition).getSpec_g_id(),mGoups.get(groupPosition).getSpec_list().get(childPosition),mInsertChildPackageDetailListener);


        return convertView;
    }

    private InsertChildPackageDetailListener mInsertChildPackageDetailListener;
    public void setInsertChildPackageDetailListener(InsertChildPackageDetailListener insertChildPackageDetailListener){
        mInsertChildPackageDetailListener = insertChildPackageDetailListener;
    }

    public interface InsertChildPackageDetailListener{

        /**
         * @param childData
         */
        void insertChildPackageDetailListener(SpecInfoModel.DataBean.SpecListBean childData);

        void insertChildImageGoDetailListener(SpecInfoModel.DataBean.SpecListBean childData);
    }
}
